<!-- 搜索引擎 -->
<!-- 它应该被包含在Setting组件中 -->
<!-- 作用就是：自定义搜索引擎 -->
<template>
    <div class="engine">
        <div class="engine-way">
                <li class="engine_selectde">默认</li>
        </div>
    </div>
    <div>
        <div class="container" v-for="engine_name in engine_names" :key="engine_name"
        :class="{ active: get_search_engine() === engine_name }" @click="set_search_engine(engine_name)">
            <div class="item">
                <p>{{engines[engine_name][3] }}</p>
                <input type="checkbox" v-model="engine_siwtch.switchList[engine_name]"  @click="cancel(engine_name)"/>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { onMounted, ref, watch} from 'vue';
import { storage } from '../utils/index';
import { engineSwitch } from '../store';
import { updateUserEngines } from '../api/post';


const search_engine = ref("")
const engine_names = ref()
const engines = ref()

onMounted(()=>{
  engines.value = storage.get("engines")
  engine_names.value = engines.value.list
})

const engine_siwtch = engineSwitch()


function cancel(engineName:string){
    engine_siwtch.switchList[engineName] = !engine_siwtch.switchList[engineName]
}


function get_search_engine() {
  return storage.get("search_engine")
}


function set_search_engine(engine: any) {
  storage.set("search_engine", engine)
  search_engine.value = engine
}

// 更新用户自定义搜索引擎
watch(engine_siwtch.switchList,()=>{
    if(storage.get("token")){
        console.log(engine_siwtch.switchList)
        updateUserEngines(engine_siwtch.switchList).then(res=>{
            if(res == "401"){
                alert("token过期")
            }
        })
    }
})

</script>

<style scoped>
.engine {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50px;
    background-color: #f2f2f2;
}
.engine-way {
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    width: 300px;
}

.engine-way li {
    list-style: none;
    padding: 10px;
    /* background-color: #bfbfbf; */
    border: 1px solid #ccc;
    border-radius: 5px;
    cursor: pointer;
}

.engine-way li:hover {
    background-color: #999999;
}

.container {
    display: flex;
    width: 100%;
    height: 35px;
    flex-direction: column;
    align-items: center;
    border-radius: 10px;
}

.item {
    display: flex;
    margin-top: auto;
    margin-bottom: auto;
    align-items: center;
    width: 100%;
    height: 35px;
    margin-top: 10px;
}

.container:hover {
    background-color: #d7d7d7;
}

.container div img {
    float: left;
    width: 24px;
    height: 24px;
}

.container div input {
    margin-left: auto;
}
</style>